﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MoodleDesktop
{
    public partial class fmForum : Form
    {

        public static List<string> listTopic = new List<string>();
        public delegate void MenuClick(object sender, EventArgs e);
        public MenuClick _itemMenu;

        private bool mousePressed = false;
        private Point diff = new Point(0, 0);


        public fmForum()
        {
            InitializeComponent();
            this.AddEventHandler(this);
        }
        
        private void AddEventHandler(Control ctl)
        {
            ctl.MouseDown += new MouseEventHandler(ctl_MouseDown);
            ctl.MouseMove += new MouseEventHandler(ctl_MouseMove);
            ctl.MouseUp += new MouseEventHandler(ctl_MouseUp);
        }

        void ctl_MouseUp(object sender, MouseEventArgs e)
        {
            mousePressed = false;
        }

        void ctl_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousePressed && (e.Button & MouseButtons.Left) != 0)
            {
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                p.X -= diff.X;
                p.Y -= diff.Y;
                DesktopLocation = p;
            }  
        }

        void ctl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mousePressed = true;

                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                diff.X = p.X - DesktopLocation.X;
                diff.Y = p.Y - DesktopLocation.Y;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            menuItem1.txt_content.Text = "Thảo luận về bài tập cá nhân";
            menuItem2.txt_content.Text = "Thảo luận về đề án";
            menuItem3.txt_content.Text = "Các thảo luận khác";
            menuItem3.btn_expand.Image = Resource1.minus;
            menuItem3.isExtand = true;

            menuItem1.mySender = new MenuItem.onUserClick(menuItem1_Click);
            menuItem2.mySender = new MenuItem.onUserClick(menuItem2_Click);
            menuItem3.mySender = new MenuItem.onUserClick(menuItem3_Click);
            //Panel_Topic.Visible = false;
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            menuItem1.mainform = this;
            menuItem2.mainform = this;
            menuItem3.mainform = this;

            //menuItem2.Location = new Point(0, 100);



        }

        private void addTopic(string heder)
        {
            Topic _topic = new Topic();
            _topic.txt_content.Text = heder;
            if ((listTopic.Count % 2) == 0)
                _topic.BackgroundImage = Resource1.listitem2;
            _topic.Location = new Point(0, listTopic.Count * 30);
            Panel_Topic.Controls.Add(_topic);
            listTopic.Add(heder);
        }

        public void menuItem2_Click(object sender, EventArgs e)
        {
            menuItem2.btn_expand.Image = Resource1.minus;
            if (menuItem1.isExtand == true)
            {
                menuItem1.btn_expand.Image = Resource1.add;
                Panel_Topic.Visible = false;
                Panel_Topic.Controls.Clear();
                menuItem2.Location = new Point(0, 34);
                //menuItem2.DiChuyenDung(34);
                menuItem3.Location = new Point(0, 361);
                //menuItem3.DiChuyenDung(361);
            }
            if (menuItem3.isExtand == true)
            {
                menuItem3.btn_expand.Image = Resource1.add;
                Panel_Topic.Visible = false;
                Panel_Topic.Controls.Clear();
                menuItem3.Location = new Point(0, 361);
                //menuItem3.DiChuyenDung(361);
                
            }
            Panel_Topic.Location = new Point(0, 68);
            listTopic.Clear();
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            Panel_Topic.Visible = true;

            menuItem2.isExtand = true;
            
            menuItem1.isExtand = false;
            menuItem3.isExtand = false;

        }

        public void menuItem1_Click(object sender, EventArgs e)
        {
            menuItem1.btn_expand.Image = Resource1.minus;
            Panel_Topic.Visible = false;
            Panel_Topic.Controls.Clear();
            if (menuItem2.isExtand == true)
            {
                menuItem2.btn_expand.Image = Resource1.add;
                menuItem2.Location = new Point(0, 327);
                //menuItem2.DiChuyenDung(327);
            }
            if (menuItem3.isExtand == true)
            {
                menuItem3.btn_expand.Image = Resource1.add;
                menuItem3.Location = new Point(0, 100);
                //menuItem3.DiChuyenDung(361);
               // System.Threading.Thread.Sleep(100);
                //menuItem2.DiChuyenDung(327);

                menuItem3.Location = new Point(0, 361);
                menuItem2.Location = new Point(0, 327);
            }
            Panel_Topic.Location = new Point(0, 34);
            listTopic.Clear();
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            
            Panel_Topic.Visible = true;

            menuItem1.isExtand = true;
            
            menuItem2.isExtand = false;
            menuItem3.isExtand = false;
        }

        public void menuItem3_Click(object sender, EventArgs e)
        {
            menuItem3.btn_expand.Image = Resource1.minus;
            Panel_Topic.Visible = false;
            Panel_Topic.Controls.Clear();
            if (menuItem1.isExtand == true)
            {
                menuItem1.btn_expand.Image = Resource1.add;
                menuItem2.Location = new Point(0, 34);
                //menuItem2.DiChuyenDung(34);
                menuItem3.Location = new Point(0, 68);
                //menuItem3.DiChuyenDung(68);
            }
            if (menuItem2.isExtand == true)
            {
                menuItem2.btn_expand.Image = Resource1.add;                
                menuItem3.Location = new Point(0, 68);
                //menuItem3.DiChuyenDung(68);

            }
            Panel_Topic.Location = new Point(0, 102);
            listTopic.Clear();
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            addTopic("abc");
            Panel_Topic.Visible = true;

            menuItem3.isExtand = true;

            menuItem2.isExtand = false;
            menuItem1.isExtand = false;
        }

        public void DiChuyenDung(int y_Dau, int y_Cuoi)
        {
            Timer t = new Timer();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
        }

        void t_Tick(object sender, EventArgs e)
        {

        }

        private void btn_close_MouseEnter(object sender, EventArgs e)
        {
            btn_close.Image = Resource1.cancel_hover;
        }

        private void btn_close_MouseHover(object sender, EventArgs e)
        {
            btn_close.Image = Resource1.cancel_hover;
        }

        private void btn_close_MouseLeave(object sender, EventArgs e)
        {
            btn_close.Image = Resource1.cancel;
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
